/**Created by the LayaAirIDE*/

module view.dlg{
	import HTMLDivElement =laya.html.dom.HTMLDivElement;
	export class Mail extends ui.dlg.MailUI{

		private nowMailData : MailData;
		private static mail : Mail;

		private constructor(){
			super();
			this.init();
		}

		static getMail() : Mail{
			if(!Mail.mail) Mail.mail = new Mail();
			return Mail.mail;
		}

		private init()
		{
			//绑定各个按钮事件
			this.btn_exit.clickHandler = new Laya.Handler(this, this.onClickExitBtn);
			this.btn_mail.clickHandler = new Laya.Handler(this, this.onClickMailBtn);
			this.btn_notice.clickHandler = new Laya.Handler(this, this.onClickNoticeBtn);
			this.btn_receive.clickHandler = new Laya.Handler(this, this.onClickReceiveBtn);
			this.mail_sv.renderHandler = new Laya.Handler(this, this.onSVRender);
			
			//先设定初始状态
			this.nomail_panel.visible = true;
			this.mail_sv.visible = false;
			this.context_panel.visible = false;
		}

		onOpened(){
			this.onClickMailBtn();
		}

		private setScrollView()
		{
			this.btn_mail.skin = "mail/btn_mail.png";
			this.btn_notice.skin = "mail/btn_notice_dark.png";
			this.titleImgs.skin = "mail/title_mail.png";

			//item属性获取
			//let itemDatas : Array<MailData> = this.getMailData();

			this.mail_sv.array = KBEngine.app.player().mail_info_list;
		}

		private getMailData()
		{
			return MailModel.GetMailList();	
		}

		private onSVRender(cell : Laya.Box, idx : number)
		{
			let optContent : Laya.Label = cell.getChildByName("optContent") as Laya.Label;
			let optTime : Laya.Label = cell.getChildByName("optTime") as Laya.Label;
			let mailInfo = this.mail_sv.array[idx];
			optTime.text = ComFun.formatTime(mailInfo.opt_time*1000,"yyyy-MM-dd hh:mm");
			let itemName = Cfg.ItemCfg[mailInfo.item_id];
			if(itemName) itemName = itemName["name"];
			else itemName = mailInfo.item_id
			optContent.text = ComFun.formatText(Cfg.MailCfg["des_"+mailInfo.interact_type+mailInfo.opt_type]["value"], {
				otherName : mailInfo.other_name,
				itemName : itemName,
				changeScore : mailInfo.change_score
			});
			if(mailInfo.opt_time > KBEngine.app.player().mail_look_time){
				optContent.color = "#b87736";
				optTime.color = "#b87736";
			}else{
				optContent.color = "#aaa8a6";
				optTime.color = "#aaa8a6";
			}
		}

		private onClickMailItem(args : MailData)
		{
			this.nowMailData = args;

			this.mail_sv.visible = false;
			this.context_panel.visible = true;
			this.nomail_panel.visible = false;

			this.context_title.text = args.title;
			this.context_text.text = args.context;
			if (args.score > 0 || args.attachmentId.length > 0)
			{
				this.attach_panel.visible = true;
				if (args.score > 0)
				{
					this.attach_score.visible = true;
					var score : Laya.Label = this.attach_score.getChildByName("score") as Laya.Label;
					score.text = "+" + args.score;
				} else
				{
					this.attach_score.visible = false;
				}

				if (args.attachmentId.length > 0)
				{
					this.attach_item.visible = true;
				} else
				{
					this.attach_item.visible = false;
				}
			} else
			{
				this.attach_panel.visible = false;
			}
			
		}

		private onClickExitBtn()
		{
			CtrlManager.GetCtrl(CtrlNames.Mail).CloseDlg();
		}

		private onClickMailBtn(){
			this.showMailPanel();
			this.setScrollView();
			CtrlManager.GetCtrl(CtrlNames.Mail).disBubble(false);
			CtrlManager.GetCtrl(CtrlNames.Mail).mailLook(false);
		}

		private showMailPanel(){
			this.btn_mail.skin = "mail/btn_mail.png";
			this.btn_notice.skin = "mail/btn_notice_dark.png";
			this.titleImgs.skin = "mail/title_mail.png";
			if (KBEngine.app.player().mail_info_list == null || KBEngine.app.player().mail_info_list.length <= 0){
				this.mail_sv.visible = false;
				this.context_panel.visible = false;
				this.nomail_panel.visible = true;
			} else{
				this.mail_sv.visible = true;
				this.context_panel.visible = false;
				this.nomail_panel.visible = false;
			}
		}

		private onClickNoticeBtn(){
			this.btn_mail.skin = "mail/btn_mail_dark.png";
			this.btn_notice.skin = "mail/btn_notice.png";
			this.titleImgs.skin = "mail/title_notice.png";
			this.mail_sv.visible = false;
			this.context_panel.visible = true;
			this.nomail_panel.visible = false;
			//设置公告

		}

		private onClickReceiveBtn()
		{
			//调用服务器函数
		}

		public ResetScrollView()
		{
			this.setScrollView();
		}
	}
}